package com.pshcp.service;

import com.framework.util.DateUtil;
import com.framework.util.SystemUtil;
import com.pshcp.domain.dto.SmsCaptchaDTO;
import com.pshcp.domain.model.SmsCaptcha;
import com.pshcp.mapper.SmsCaptchaMapper;

import com.github.pagehelper.PageHelper;
import com.framework.base.PageList;
import com.framework.tool.CommonTool;
import com.pshcp.common.enums.CommonStatusEnum;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 短信验证码信息表 服务实现类
 * </p>
 *
 * @author auto generator
 * @since 2022-11-02
 */
@Service
public class SmsCaptchaService {

    @Autowired
    private SmsCaptchaMapper smsCaptchaMapper;

    public SmsCaptcha getLatestUnUseSmsCaptcha(Integer terminal, Integer purpose, String phone) {
        Example example = new Example(SmsCaptcha.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("terminal", terminal);
        criteria.andEqualTo("purpose", purpose);
        criteria.andEqualTo("phone", phone);
        criteria.andEqualTo("bizStatus", 0);
        criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        example.setOrderByClause(" created_time desc");
        List<SmsCaptcha> list = smsCaptchaMapper.selectByExample(example);
        return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }

    public PageList<SmsCaptcha> list(SmsCaptchaDTO smsCaptchaDTO) {
        if (smsCaptchaDTO.getPageNum() != null && smsCaptchaDTO.getPageSize() != null) {
            PageHelper.startPage(smsCaptchaDTO.getPageNum(), smsCaptchaDTO.getPageSize());
        }
        Example example = new Example(SmsCaptcha.class);
        Example.Criteria criteria = example.createCriteria();
        if (smsCaptchaDTO.getStatus() != null) {
            criteria.andEqualTo("status", smsCaptchaDTO.getStatus());
        }
        example.setOrderByClause(" created_time desc");
        List<SmsCaptcha> list = smsCaptchaMapper.selectByExample(example);
        return CommonTool.buildPageList(list);
    }

    public List<SmsCaptcha> select(SmsCaptcha smsCaptcha) {
        return smsCaptchaMapper.select(smsCaptcha);
    }

    public int insert(SmsCaptcha smsCaptcha) {
        smsCaptcha.setId(SystemUtil.getId());
        smsCaptcha.setStatus(CommonStatusEnum.INIT.getCode());
        smsCaptcha.setCreatedTime(new Date());
        return smsCaptchaMapper.insert(smsCaptcha);
    }

    public int update(SmsCaptcha smsCaptcha) {
        smsCaptcha.setUpdateTime(new Date());
        return smsCaptchaMapper.updateByPrimaryKeySelective(smsCaptcha);
    }

    public SmsCaptcha findById(Long id) {
        return smsCaptchaMapper.selectByPrimaryKey(id);
    }

    public List<SmsCaptcha> findByIds(List<Long> ids) {
        Example example = new Example(SmsCaptcha.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", ids);
        return smsCaptchaMapper.selectByExample(example);
    }

    public int delById(Long id) {
        Example example = new Example(SmsCaptcha.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);

        SmsCaptcha smsCaptcha = new SmsCaptcha();
        smsCaptcha.setStatus(CommonStatusEnum.DEL.getCode());
        return smsCaptchaMapper.updateByExampleSelective(smsCaptcha, example);
    }

}
